package com.wei.czz.framework.common.controller;

import com.wei.czz.common.annotation.Auth;
import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.common.limitConfig.LimitConfigDto;
import com.wei.czz.common.jsr303.group.OrderSearch;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.common.BaseVo;
import com.wei.czz.framework.common.manager.LimitConfigManager;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-08 13:59:23
 * className: LimitConfigFlowController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/sys/limit-config-flow")
@AllArgsConstructor
public class LimitConfigFlowController {

    private static final Logger log = LoggerFactory.getLogger(LimitConfigFlowController.class);

    private final LimitConfigManager limitConfigManager;

    /**
     * 分页获取限流配置流水接口
     * @param masterId 主表主键
     * @param baseVo   参数对象
     * @return 结果
     */
    @Resubmit(spEL = "'getLimitConfigHistoryPageList' + #userPo.userId + '-' + #baseVo")
    @PostMapping("/page/list")
    @Auth(permit = "limit:config:list")
    public Result<PageDto<LimitConfigDto>> getLimitConfigHistoryPageList(
            @RequestParam("masterId") Long masterId,
            @Validated(OrderSearch.class) @RequestBody BaseVo baseVo) {
        log.info("开始分页获取限流配置历史记录业务。masterId={} {}", masterId, baseVo);

        PageDto<LimitConfigDto> pageDto = limitConfigManager.getLimitConfigHistoryPageList(masterId, baseVo);

        log.info("分页获取限流配置历史记录业务结束");
        return Result.success(pageDto);
    }


    /**
     * 使用限流配置流水接口
     * 覆盖正在使用的限流配置
     * @param id 主键
     * @return 结果
     */
    @Resubmit(spEL = "'useLimitConfigFlow' + #userPo.userId")
    @PostMapping("/use")
    @Auth(permit = "limit:config:save")
    public Result<Object> useLimitConfigFlow(@RequestParam("id") Long id) {
        log.info("开始使用限流配置流水业务。id={}", id);

        limitConfigManager.useLimitConfigFlow(id);

        log.info("使用限流配置流水业务结束");
        return Result.success();
    }
}
